﻿<div id="breadcrumb">
    <a href="#"><i class="fa fa-bookmark"></i> 系统管理</a>
    <a href="#" class="current">参数设置</a>
</div>
<div class="row">
    <div class="col-xs-2">
        <button id="btnsave" class="btn btn-small btn-primary">保存</button>
        <button id="btnclearcache" class="btn btn-small btn-success">清除缓存</button>
    </div>
</div>
<div class="row" data-autosize="true">
    <div class="col-xs-12">
        @using (Html.BeginForm("", "", FormMethod.Post, new { @class = "form-horizontal form-inline", id = "inputform" }))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })

            <table class="table table-condensed responsive grid-edit" id="list_table" style="width:auto;">
                <thead>
                    <tr>
                        <td width="300">Key</td>
                        <td width="300">Value</td>
                        <td width="300">Remark</td>
                        <td width="30"></td>
                    </tr>
                </thead>
                <tbody class="list-template hidden">
                    <tr>
                        <td>@Html.TextBox("Parametername", "", new { @class = "grid-control list-datafield", @data_fieldname = "Parametername" })</td>
                        <td>@Html.TextBox("Parametervalue", "", new { @class = "grid-control list-datafield", @data_fieldname = "Parametervalue" })</td>
                        <td>@Html.TextBox("Remark", "", new { @class = "grid-control list-datafield", @data_fieldname = "Remark" })</td>
                        <td align="center">
                            <button id="btndelrow" class="btn btn-mini"><i class="fa fa-times"></i></button>
                        </td>
                    </tr>
                </tbody>
                <tbody class="list-data"></tbody>
                <tbody class="list-deleted hidden"></tbody>
                <tbody>
                    <tr>
                        <td class="pull-left">
                            <a href="#none" class="btn btn-mini btn-default addRow">添加行</a>
                        </td>
                    </tr>
                </tbody>
            </table>
        }
    </div>
</div>

@section scripts{
    <script type="text/javascript">
        var datajson = @Html.Raw(ViewBag.DataJson);

        $(document).ready(function () {
            //表单验证
            $('#inputform').validate({});

            //保存
            $("#btnsave").click(function (evt) { evt.preventDefault(); save($(this)); });

            //取消
            $("#btncancel").click(function (evt) { parent.closeDialog(); return false; });

            //添加行
            $(".addRow").click(function (evt) {
                addRow();
            });

            //清除缓存
            $("#btnclearcache").click(function (evt) {
                evt.preventDefault();

                $.ajax({
                    type: "post",
                    url: baseurl + "Manage/Setting/ClearCache",
                    success: function (e) {
                        $.messager.alert(csms_lang.message_info_title, e, "info");
                    }
                });
            });

            //加载项目明细
            loadDtls();
        });

        //初始化表单验证
        function initValidate() {
            //表单验证
            $('#inputform').validate({});
        }

        //选择子项目字典库
        function addInspectItem(jsonstr) {
            var jsondata = JSON.parse(jsonstr);

            for (var i = 0; i < jsondata.length; i++) {
                jsondata[i].Id = 0;
                addRow(jsondata[i]);
            }
        }

        //添加行
        function addRow(data) {
            //在列表中添加一行
            var row = $("#list_table .list-template tr").eq(0).clone();
            row.appendTo("#list_table .list-data");

            //行ID
            $(row).attr("data-id", "0");
            //为每个控件生成动态id和name，否则表单验证会有问题
            var newid = (new Date()).getMilliseconds();
            $(row).find(".list-datafield").each(function () {
                $(this).attr("id", $(this).attr("id") + "_" + newid);
                $(this).attr("name", $(this).attr("name") + "_" + newid);
            });
            //为每行添加验证规则
            $(row).find("[data-fieldname='Parametername']").rules("add", { required: true });

            //行数据
            if (data) {
                //行ID
                $(row).attr("data-id", data.Id);
                $(row).find("[data-fieldname='Parametername']").attr("disabled",true);
                //行数据
                $(row).find(".list-datafield").each(function () {
                    $(this).val(data[$(this).attr("data-fieldname")]);
                });
            }
            //删除行事件
            $(row).find("#btndelrow").click(function (evt) {
                evt.preventDefault();
                var rowId = $(row).attr("data-id");
                if (rowId == "0") {
                    //如果是新加的行，则直接移除
                    $(row).remove();
                } else {
                    //否则移到隐藏区域，等待保存
                    $(row).appendTo($("#list_table .list-deleted"));
                }
            });
        }

        //加载明细数据
        function loadDtls() {
            for (var i = 0; i < datajson.length; i++) {
                addRow(datajson[i]);
            }
        }

        //保存数据
        function save(obj) {
            //表单验证
            if (!$("#inputform").valid()) {
                return;
            }

            var jsondata = {
                Rows: [],
                DeletedRows: []
            };
            //明细数据
            $("#list_table .list-data tr").each(function () {
                var rowdata = {
                    Id: $(this).attr("data-id"),
                    Values: {}
                };

                //行数据
                $(this).find(".list-datafield").each(function () {
                    rowdata.Values[$(this).attr("data-fieldname")] = $(this).val();
                });

                jsondata.Rows.push(rowdata);
            });

            //已删除的检查明细数据
            $("#list_table .list-deleted tr").each(function () {
                jsondata.DeletedRows.push($(this).attr("data-id"));
            });

            //禁用保存按钮，防止重复提交
            $(obj).attr("disabled", true);

            //保存数据
            $.ajax({
                type: "post",
                url: baseurl + "Manage/Setting/Save",
                data: { json: JSON.stringify(jsondata) },
                success: function (e) {
                    $(obj).attr("disabled", false);
                    if (e.Ret == "0") {
                        $.messager.alert(csms_lang.message_info_title, csms_lang.message_save_success, "info",function(){
                            location.href = location.href;
                        });
                    } else {
                        $.messager.alert(csms_lang.message_error, e.Msg, "error");
                    }
                }
            });
        }
    </script>
}
